package com.itheima.easy.controller.customer;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.itheima.easy.base.AjaxResult;
import com.itheima.easy.base.PageResponse;
import com.itheima.easy.controller.BaseController;
import com.itheima.easy.utils.UserThreadLocal;
import com.itheima.easy.dto.ReservationDto;
import com.itheima.easy.service.ReservationService;
import com.itheima.easy.vo.ReservationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
public class CustomerReservationController extends BaseController {

    @Autowired
    private ReservationService reservationService;

    @PostMapping
    @ApiOperation("新增预约")
    public AjaxResult add(@RequestBody ReservationDto dto) {
        reservationService.add(dto);
        return success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除预约")
    public AjaxResult deleteById(@PathVariable Long id) {
        reservationService.deleteById(id);
        return success();
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public AjaxResult cancel(@PathVariable Long id) {
        reservationService.cancelReservation(id);
        return success();
    }


    @GetMapping("/{id}")
    @ApiOperation("根据ID查询预约")
    public AjaxResult<ReservationVo> findById(@PathVariable Long id) {
        ReservationVo reservationVO = reservationService.findById(id);
        return success(reservationVO);
    }

    @GetMapping
    @ApiOperation("查询所有预约")
    public AjaxResult<List<ReservationVo>> findAll(@RequestParam(required = false) String mobile, @RequestParam(required = false) Long time) {
        List<ReservationVo> reservationVoList = reservationService.findAll(mobile, LocalDateTimeUtil.of(time));
        return success(reservationVoList);
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public AjaxResult countReservationsForEachTimeWithinTimeRange(@RequestParam(required = false) Long time) {
        return success(reservationService.countReservationsForEachTimeWithinTimeRange(LocalDateTimeUtil.of(time)));

    }

/*
    *分页查询增加预约人姓名，手机号，状态，类型的查询条件
     */
    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "预约人姓名", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", required = false, dataType = "int", paramType = "query")
    })
    public AjaxResult<PageResponse<ReservationVo>> findByPage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize,
                                                    @RequestParam(required = false) String name,
                                                    @RequestParam(required = false) String phone,
                                                    @RequestParam(required = false) Integer status,
                                                    @RequestParam(required = false) Integer type) {
        PageResponse<ReservationVo> byPage = reservationService.findByPage(pageNum, pageSize, name, phone, status, type, null, null);
        return success(byPage);
    }

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public AjaxResult<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        return success(reservationService.getCancelledReservationCount(userId));
    }


}
